# 21_jes2012_subjective_position.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-7-19 Taka-aki Asano
# Last Modified: 2022-1-19

# package
require("haven")
require("dplyr")
require("tidyr")
require("ggplot2")
quartzFonts(HiraKaku = quartzFont(rep('Hiragino Kaku Gothic Pro W3', 4)))
theme_set(theme_classic(base_size = 12, base_family = 'HiraKaku'))


# 2012
## merge
JES2012_Ideology <- JES2012_Respondent[,c("id", "Ideology")] %>% 
  left_join(JES2012[,c("id", "f1", "f4", "f2", "q4", "q60s2_2", "q60s2_6", 
                       "q60s2_3", "q60s2_7", "q60s2_9", "q60s2_11", 
                       "q60s2_13", "q60s2_15", "q17", "q54")], 
            by = "id")

## plot
Ideology_Compare <- ggplot(
  JES2012_Ideology[!is.na(JES2012_Ideology$q17),], 
  aes(x = as.factor(q17), y = Ideology)) + 
  geom_boxplot() + 
  labs(x = expression(Progressive  %<-%  "Subjective Ideological Position" %->% Conservative), 
       y = expression(Left  %<-%  "Objective Ideological Position" %->% Right), 
       title = "(a) All Respondents") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(Ideology_Compare)

Ideology_Compare_Young <- ggplot(
  JES2012_Ideology[!is.na(JES2012_Ideology$q17) & JES2012_Ideology$f2 <= 39,], 
  aes(x = as.factor(q17), y = Ideology)) + 
  geom_boxplot() + 
  labs(x = expression(Progressive  %<-%  "Subjective Ideological Position" %->% Conservative), 
       y = expression(Left  %<-%  "Objective Ideological Position" %->% Right), 
       title = "(b) Respondents Aged 39 or Younger") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(Ideology_Compare_Young)

Ideology_Compare_Old <- ggplot(
  JES2012_Ideology[!is.na(JES2012_Ideology$q17) & JES2012_Ideology$f2 >= 60,], 
  aes(x = as.factor(q17), y = Ideology)) + 
  geom_boxplot() + 
  labs(x = expression(Progressive  %<-%  "Subjective Ideological Position" %->% Conservative), 
       y = expression(Left  %<-%  "Objective Ideological Position" %->% Right), 
       title = "(c) Respondents Aged 60 or Older") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(Ideology_Compare_Old)

## recode
JES2012_Ideology$Vote <- NA
JES2012_Ideology$Vote[JES2012_Ideology$q4 == 1] <- 1
JES2012_Ideology$Vote[JES2012_Ideology$q4 == 2] <- 0
JES2012_Ideology$q60s2_2[is.na(JES2012_Ideology$q60s2_2)] <- 0
JES2012_Ideology$q60s2_6[is.na(JES2012_Ideology$q60s2_6)] <- 0
JES2012_Ideology$Campaign <- JES2012_Ideology$q60s2_2 + JES2012_Ideology$q60s2_6
JES2012_Ideology$q60s2_3[is.na(JES2012_Ideology$q60s2_3)] <- 0
JES2012_Ideology$q60s2_7[is.na(JES2012_Ideology$q60s2_7)] <- 0
JES2012_Ideology$q60s2_9[is.na(JES2012_Ideology$q60s2_9)] <- 0
JES2012_Ideology$System <- JES2012_Ideology$q60s2_3 + JES2012_Ideology$q60s2_7 + JES2012_Ideology$q60s2_9
JES2012_Ideology$q60s2_11[is.na(JES2012_Ideology$q60s2_11)] <- 0
JES2012_Ideology$q60s2_13[is.na(JES2012_Ideology$q60s2_13)] <- 0
JES2012_Ideology$Demo <- JES2012_Ideology$q60s2_11 + JES2012_Ideology$q60s2_13
JES2012_Ideology$q60s2_15[is.na(JES2012_Ideology$q60s2_15)] <- 0
JES2012_Ideology$q17_Strong <- abs(JES2012_Ideology$q17 - 6)
JES2012_Ideology$Support <- ifelse(JES2012_Ideology$q54 == 15, 0, 1)
JES2012_Ideology$Female <- ifelse(JES2012_Ideology$f1 == 2, 1, 0)
JES2012_Ideology$f4[JES2012_Ideology$f4 == 9] <- NA
JES2012_Ideology$College <- ifelse(JES2012_Ideology$f4 == 4, 1, 0)

## regression
vote2012 <- glm(
  Vote ~ q17 + q17_Strong + Support + Female + College + f2, 
  data = JES2012_Ideology, family = binomial(link = "logit")
)
summary(vote2012)

campaign2012 <- glm(
  Campaign ~ q17 + q17_Strong + Support + Female + College + f2, 
  data = JES2012_Ideology, family = "poisson"
)
summary(campaign2012)

system2012 <- glm(
  System ~ q17 + q17_Strong + Support + Female + College + f2, 
  data = JES2012_Ideology, family = "poisson"
)
summary(system2012)

demo2012 <- glm(
  Demo ~ q17 + q17_Strong + Support + Female + College + f2, 
  data = JES2012_Ideology, family = "poisson"
)
summary(demo2012)

online2012 <- glm(
  q60s2_15 ~ q17 + q17_Strong + Support + Female + College + f2, 
  data = JES2012_Ideology, family = binomial(link = "logit")
)
summary(online2012)
